<?php
/**
 * @package    Blog
 * @subpackage Model
 */

class Customer
{
 
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
    
    public function listCustomers()
    {
    	$cust_arr = $this->_db->fetchAll(
    		"SELECT C.customer_id, C.customer_code, C.customer_name, C.customer_logo, C.project_type, C.location, C.status, C.created_date, L.city_name
    		 FROM customer C
    		 LEFT JOIN cities L ON L.city_id = C.location	
    		");
    		
    	return $cust_arr;	
    }
    
    public function getCustomerLogo($customer_id)
    {
    	$customer_logo = $this->_db->fetchRow("
    		SELECT customer_logo 
    		FROM customer
    		WHERE customer_id = " . $customer_id);
    	
    	return $customer_logo;
    }
    
    public function addCustomer($formValues)
    {
    	$customer_code = $formValues['customer_code'];
		$customer_name = $formValues['customer_name'];
		$project_type = $formValues['project_type'];
		$location = $formValues['location'];
		$status = $formValues['status'];
		
		$add_customer = $this->_db->exec(
			"INSERT INTO customer(customer_code, customer_name, project_type, location, status, created_date)
			 VALUES('" . $customer_code . "', '" . $customer_name . "', '" . $project_type . "', '" . $location . "', " . $status . ", now())
			");
		
		$max_cust_id = $this->_db->fetchRow("SELECT last_insert_id() AS MAX_CUST_ID FROM customer");
		$last_customer_id = $max_cust_id['MAX_CUST_ID'];
		
        if($add_customer)
        {
        	echo "New Customer has been successfully created|" . $last_customer_id;        	
        }	
    }
    
    public function updateCustomerLogo($filename, $cust_id)
    {
    	$update_logo = $this->_db->exec(
    		"UPDATE customer SET customer_logo = '" . $filename . "' 
    		WHERE customer_id = " . $cust_id);
    }
    
    public function getCustomerbyId($customer_id)
    {
    	$customer_arr = $this->_db->fetchRow(
    		"SELECT customer_code, customer_name, customer_logo, project_type, location, status, created_date
    		 FROM customer
    		 WHERE customer_id = " . $customer_id);
    	
    	return $customer_arr;		
    }
    
    public function updateCustomer($formValues)
    {
    	$customer_id = $formValues['customer_id'];
    	$customer_code = $formValues['customer_code'];
		$customer_name = $formValues['customer_name'];
		$project_type = $formValues['project_type'];
		$location = $formValues['location'];
		$status = $formValues['status'];
		
		$update_customer = $this->_db->exec(
			"UPDATE customer 
			 SET customer_code = '" . $customer_code . "', customer_name = '" . $customer_name . "', project_type = '" . $project_type . "', 
			 	 location = '" . $location . "', status = " . $status . ", created_date = now()
			 WHERE customer_id = " . $customer_id);
		
		if($update_customer)
		{
			echo "Customer details has been successfully updated.";
		}
    }
    
    public function delCustomer($customer_id)
    {
    	$customer_logo = $this->getCustomerbyId($customer_id);
    	
    	$del_customer_logo = unlink("images/clients/" . $customer_logo['customer_logo']);
    	if($del_customer_logo)
    	{
    		$del_customer_logo_thumb = unlink("images/clients/thumbs/" . $customer_logo['customer_logo']);
    		if($del_customer_logo_thumb) {
    			$delCust = $this->_db->exec(
	    		"DELETE FROM customer
	    		 WHERE customer_id = " . $customer_id);
    		}
    	}
    }
    
    public function listCustomersByUserId($userid)
    {
    	$cust_arr = $this->_db->fetchAll(
    		"SELECT C.customer_id, C.customer_name
			 FROM customer C
			 LEFT JOIN projects P ON P.customer_id = C.customer_id
			 WHERE P.bse_id = " . $userid);
    		
    	return $cust_arr;	
    }
}